
package suncertify.shared;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import suncertify.Server.ServerThread;


/**
 * Event Handler class for <code>ServerConfigUI</code> screen, it sets the properties entered from <code>ServerConfigUI</code> and starts the Runnable <code>ServerThread</code> which starts the RMI Server
 * @author jshah
 */
public class RMIConfigController implements ActionListener{
    
    ConfigUI instance ;
    ServerProperty propertyClass;

    /**
     * flag to determine whether to display the config screen or not
     */
    public static boolean showScreen = true;

    /**
     * Stores an instance of the <code>SeverConfigUI</code> instance whose events are being handled
     * @param instance    A <code>ServerConfigUI</code>
     */
    public void setInstance(ConfigUI instance) {
        this.instance = instance;
    }

    public void actionPerformed(ActionEvent e) {

        if(e.getSource()== this.instance.getSubmitButton()){

            String host = this.instance.getRmiHost_field().getText();
            String url = this.instance.getRemoteUrlField().getText();
            String port = this.instance.getRmiPort_field().getText();

            if(!this.validateInput(host, url, port)){
                JOptionPane.showMessageDialog(instance.getClientConfigFrame().getContentPane(), "INVALID ENTRY");
            }
            else{
                this.writeProperties(host, url, port);
            }

            
        }

        if(e.getSource()==this.instance.getDefaultButton()){
            String rmiUrl = "//"+ServerProperty.getDefaultRmiHostProp()+":"+ServerProperty.getDefaultRmiPortProp()+"/"+ServerProperty.getDefaultRmiUrlProp();
            this.configureServer(rmiUrl);
        }

    }

    //validate the value entered in the host, url and port TextFields to make sure it's not an invalid input
    private boolean validateInput(String host,String url,String port){
        int portid = 0;
        if(host==null || host.isEmpty())
            return false;
        if(url==null || url.isEmpty())
            return false;
        try{
            portid = Integer.parseInt(port);
            if(portid<1000 || portid>9999)
                return false;

        }catch(NumberFormatException ne){
            return false;
        }
        return true;

    }


    /**
     * write the values to respective properties in the properties file
     * @param host  name for the rmi host
     * @param url   location for the rmi object in the host
     * @param port  port at which the rmi object is listening
     */
    public void writeProperties(String host, String url, String port){
               
                ServerProperty.setServerRmiHostProp(host);
                ServerProperty.setServerRmiUrlProp(url);
                ServerProperty.setSeverRmiPortProp(port);

                //the last used properties will become the default properties for future use
                ServerProperty.setReusableProp(host, url, port);
                //make sure all the porperties are properly written to the properites file
                ServerProperty.storeProps();
                
                String rmiUrl = "//"+ServerProperty.getServerRmiHostProp()+":"+ServerProperty.getServerRmiPortProp()+"/"+ServerProperty.getServerRmiUrlProp();
                this.configureServer(rmiUrl);
                
    }
    

    /**
     * Sets the RMI url to start the rmi server for the remote object
     * @param rmiUrl    specifies the location at which the remote object is located
     */
    public void configureServer(String rmiUrl){
                Thread t  = new Thread(new ServerThread(rmiUrl));
                t.start();
                this.instance.getClientConfigFrame().setVisible(false);
                RMIConfigController.showScreen = false;
    }
}
